package com.zjitc.controller;

import com.github.pagehelper.PageInfo;
import com.zjitc.model.Category;
import com.zjitc.model.Manage;
import com.zjitc.model.Order;
import com.zjitc.model.Product;
import com.zjitc.model.User;
import com.zjitc.service.ManageService;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/5/28 16:48
 * @description:
 */
@Controller
@RequestMapping("/manage")
public class ManageController {

  @Autowired
  private ManageService manageService;

  @RequestMapping("/toTop")
  public String toTop() {
    return "admin/top";
  }

  @RequestMapping("/toBottom")
  public String toBottom() {
    return "admin/bottom";
  }

  @RequestMapping("/toWelcome")
  public String toWelcome() {
    return "admin/welcome";
  }

  @RequestMapping("/toLeft")
  public String toLeft() {
    return "admin/left";
  }

  //==============================================用户管理=============================//
  /**
   * 进入登陆页面
   * @return
   */
  @RequestMapping(value = "/toLogin")
  private String toLogin(){
    return "admin/login";
  }

  /**
   * 管理员登陆
   * @param mname
   * @param mpassword
   * @param session
   * @return
   */
  @RequestMapping(value = "/manageLogin")
  private String manageLogin(
      @RequestParam(value = "mname", required = false) String mname,
      @RequestParam(value = "mpassword", required = false) String mpassword,
      HttpSession session
  ){
    Manage manage =  manageService.manageLoginByMname(mname);
    if (manage.getMname() != null){
      manage = manageService.manageLogin(mname,mpassword);
      if (manage != null){
        session.setAttribute("manage",manage);
        return "admin/home";
      }else {
        return null;
      }
    }
    return "admin/login";
  }

  /**
   * 显示所有用户
   * @param pageNum
   * @param count
   * @param session
   * @return
   */
  @RequestMapping(value = "/displayUserList")
  private String displayUserList(
      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
      @RequestParam(value = "count", defaultValue = "10") Integer count,
      HttpSession session
  ){
    List<User> userList = manageService.findAllUser(pageNum,count);
    PageInfo<User> pageInfo = new PageInfo<User>(userList);
    session.setAttribute("user",userList);
    session.setAttribute("page",pageInfo);
    return "admin/user/list";
  }

  /**
   * 删除用户
   * @param uid
   * @param session
   * @return
   */
  @RequestMapping(value = "/deleteUser")
  private String deleteUser(Integer uid, HttpSession session){
    manageService.deleteUser(uid);
    return "redirect:/manage/displayUserList.do";
  }

  //====================================================商品管理============================================================//

  //====================================================商品大名字显示，修改===============================================//
  /**
   * 显示一级商品分类
   * @param pageNum
   * @param count
   * @param session
   * @return
   */
  @RequestMapping(value = "/displayOneProduct")
  private String displayProduct(
      @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
      @RequestParam(value = "count",defaultValue = "10") Integer count,
      HttpSession session
  ){
    List<Category> categoryList = manageService.findOneProduct(pageNum,count);
    PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
    session.setAttribute("category",categoryList);
    session.setAttribute("page",pageInfo);
    return "admin/category/list";
  }

  /**
   * 进入修改商品名字页面
   * @param cid
   * @param session
   * @return
   */
  @RequestMapping(value = "/editOneProduct")
  private String editOneProduct(String cid,HttpSession session){
    Category category = manageService.findCid(cid);
    //session里面的内容不会消失，如果要跳回来，要重定向回来
    session.setAttribute("category",category);
    return "admin/category/edit";
  }

  /**
   * 进行修改功能
   * @param category
   * @param session
   * @return
   */
  @RequestMapping(value = "/updateOneProduct")
  private String updateOneProduct(Category category, HttpSession session){
    manageService.updateOneProduct(category);
    //重定向到显示页面
    return "redirect:/manage/displayOneProduct.do";
  }

  //====================================================商品全部信息显示、修改===================================//
  /**
   * 查询所有商品
   * @param pageNum
   * @param count
   * @param session
   * @return
   */
  @RequestMapping(value = "/displayTwoProduct")
  private String displayTwoProduct(
      @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
      @RequestParam(value = "count",defaultValue = "10") Integer count,
      HttpSession session
  ){
    //List<productList>从数据库中找到数据
    List<Product> productList = manageService.findTwoProduct(pageNum,count);
    PageInfo<Product> pageInfo = new PageInfo<>(productList);
    //将找到的数据传到页面
    session.setAttribute("productList",productList);
    session.setAttribute("page",pageInfo);
    return "admin/product/list";
  }

  /**
   * 进入商品修改页面
   * @param pageNum
   * @param count
   * @param pid
   * @param session
   * @return
   */
  @RequestMapping(value = "/editTwoProduct")
  private String editTwoProduct(
      @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
      @RequestParam(value = "count",defaultValue = "10") Integer count,
      Integer pid,
      HttpSession session
  ){
    Product product = manageService.findPid(pid);
    List<Category> categoryList = manageService.findOneProduct(pageNum,count);
    session.setAttribute("product",product);
    session.setAttribute("categoryList",categoryList);
    return "admin/product/edit";
  }

  /**
   * 修改商品信息，无法改变图片
   * @param product
   * @return
   */
  @RequestMapping(value = "/updateTwoProduct")
  private String updateTwoProduct(Product product){
    manageService.updateTwoProduct(product);
    //重定向到显示页面
    return "redirect:/manage/displayTwoProduct.do";
  }

  /**
   * 删除商品
   * @param pid
   * @return
   */
  @RequestMapping(value = "/deleteTwoProduct")
  private String deleteTwoProduct(Integer pid){
      manageService.deleteTwoProduct(pid);
      return "redirect:/manage/displayTwoProduct.do";
  }

  //==============================================================订单显示======================//
  /**
   * 显示订单信息
   * @param pageNum
   * @param count
   * @param session
   * @return
   */
  @RequestMapping(value = "/orderList")
  private String orderList(
      @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
      @RequestParam(value = "count",defaultValue = "10") Integer count,
      HttpSession session
  ){
    List<Order> orderList = manageService.findOrderList(pageNum,count);
    PageInfo<Order> pageInfo = new PageInfo<>(orderList);
    session.setAttribute("orderList",orderList);
    session.setAttribute("pageInfo",pageInfo);
    return "admin/order/list";
  }
}
